﻿#region DaUserRole
/*----------------------------------------------------------------
// 文件名：DaUserRole.cs
// 功能描述：表dbo.User_Role的数据访问层
//
// 
// 创建时间：2009-11-20 DtataAccess template . Ver 5.0.20090413
//
// 修改标识： 
// 修改描述： 
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using EQT.Model;


namespace EQT.Dal
{
    ///可以继承更上层接口,IUserRole ,数据访问层基类DaBase，
    ///DaBase中包含了DbException异常处理静态方法
    public partial class DaUserRole : DaBase<MoUserRole>
    {
        #region SQL const
        internal const string TABLE_COLUMNS = " UID ,ROLE_ID   ";

        internal const string SQL_INSERT = "INSERT INTO USER_ROLE (" + TABLE_COLUMNS + ") VALUES (@Uid,@RoleId)";
        internal const string SQL_SELECT = "SELECT " + TABLE_COLUMNS + " FROM USER_ROLE  ";
        internal const string SQL_SELECT_ONE = SQL_SELECT + " WHERE UID=@Uid AND  ROLE_ID=@RoleId";

        internal const string SQL_EXIST = "SELECT COUNT(*) FROM USER_ROLE  WHERE UID=@Uid and  ROLE_ID=@RoleId  ";
        internal const string SQL_UPDATE = "UPDATE USER_ROLE SET WHERE UID=@Uid AND  ROLE_ID=@RoleId";

        internal const string SQL_DELETE_DEFAULT = "DELETE FROM USER_ROLE ";
        internal const string SQL_DELETE = "DELETE FROM USER_ROLE WHERE UID=@Uid AND  ROLE_ID=@RoleId";

        internal const string SQL_COUNT = "SELECT COUNT(*) FROM User_Role ";
        #endregion

        #region Constructor

        /// <summary>
        /// 默认构造函数，使用配置文件中默认的数据库配置。
        /// </summary>
        public DaUserRole()
        {
            this.db = DatabaseFactory.CreateDatabase();
        }
        /// <summary>
        /// 使用指定的databaseName数据库配置。
        /// </summary>
        /// <param name="databaseName">配置文件中数据库的配置名称。</param>
        public DaUserRole(string databaseName)
        {
            this.db = DatabaseFactory.CreateDatabase(databaseName);
        }

        public DaUserRole(Database db)
        {
            this.db = db;
        }

        #endregion

        //以下是CRUD方法，每个方法都有引入外部事务的版本

        #region 构造器模块

        internal override MoUserRole ConstructT()
        {
            return new MoUserRole();
        }
        #endregion



        #region Add模块 Helper

        ///build the command object.It never throw exception.
        internal override DbCommand ConstructAddCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);

            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString);
            db.AddInParameter(dbCommand, "RoleId", DbType.AnsiString);
            return dbCommand;
        }
        internal override DbCommand ConstructAddCommand(Database db, MoUserRole entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);
            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString, entity.Uid);
            db.AddInParameter(dbCommand, "RoleId", DbType.AnsiString, entity.RoleId);
            return dbCommand;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, MoUserRole entity)
        {
            db.SetParameterValue(addCmd, "Uid", entity.Uid);
            db.SetParameterValue(addCmd, "RoleId", entity.RoleId);
            return addCmd;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, DataRow row)
        {
            db.SetParameterValue(addCmd, "Uid", row["Uid"]);
            db.SetParameterValue(addCmd, "RoleId", row["RoleId"]);
            return addCmd;
        }

        #endregion

        #region Update模块 Helper

        internal override DbCommand ConstructUpdateCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString);
            db.AddInParameter(dbCommand, "RoleId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructUpdateCommand(Database db, MoUserRole entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString, entity.Uid);
            db.AddInParameter(dbCommand, "RoleId", DbType.AnsiString, entity.RoleId);

            return dbCommand;
        }

        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, MoUserRole entity)
        {
            db.SetParameterValue(updateCmd, "Uid", entity.Uid);
            db.SetParameterValue(updateCmd, "RoleId", entity.RoleId);
            return updateCmd;
        }
        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, DataRow row)
        {
            db.SetParameterValue(updateCmd, "Uid", row["Uid"]);
            db.SetParameterValue(updateCmd, "RoleId", row["RoleId"]);
            return updateCmd;
        }

        internal override void PrepareDataAdapterCommand(Database db, out DbCommand dbInsertCommand,
                             out DbCommand dbUpdateCommand, out DbCommand dbDeleteCommand)
        {
            dbInsertCommand = db.GetSqlStringCommand(SQL_INSERT);
            #region set insert cmd parameters
            db.AddInParameter(dbInsertCommand, "Uid", DbType.AnsiString, "UID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "RoleId", DbType.AnsiString, "ROLE_ID", DataRowVersion.Current);
            #endregion
            dbUpdateCommand = db.GetSqlStringCommand(SQL_UPDATE);
            #region Set update cmd value parameters
            #endregion
            #region set update cmd pk where parameters
            db.AddInParameter(dbUpdateCommand, "Uid", DbType.AnsiString, "UID", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "RoleId", DbType.AnsiString, "ROLE_ID", DataRowVersion.Current);
            #endregion
            dbDeleteCommand = db.GetSqlStringCommand(SQL_DELETE);
            #region set delete cmd pk where parameters
            db.AddInParameter(dbDeleteCommand, "Uid", DbType.AnsiString, "UID", DataRowVersion.Current);
            db.AddInParameter(dbDeleteCommand, "RoleId", DbType.AnsiString, "ROLE_ID", DataRowVersion.Current);
            #endregion
        }
        #endregion

        #region Delete模块 Helper

        internal override DbCommand ConstructDeleteCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString);
            db.AddInParameter(dbCommand, "RoleId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructDeleteCommand(Database db, MoUserRole entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString, entity.Uid);
            db.AddInParameter(dbCommand, "RoleId", DbType.AnsiString, entity.RoleId);

            return dbCommand;
        }

        internal override DbCommand ConstructDeleteCommandForWhere(Database db, string where)
        {
            return db.GetSqlStringCommand(SQL_DELETE_DEFAULT + where);
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, MoUserRole entity)
        {
            db.SetParameterValue(deleteCmd, "Uid", entity.Uid);
            db.SetParameterValue(deleteCmd, "RoleId", entity.RoleId);
            return deleteCmd;
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, DataRow row)
        {
            db.SetParameterValue(deleteCmd, "Uid", row["Uid"]);
            db.SetParameterValue(deleteCmd, "RoleId", row["RoleId"]);
            return deleteCmd;
        }

        #endregion helper

        #region Query模块 Helper

        internal override DbCommand ConstructQueryCommand(string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region GetEntity(s)模块 Helper

        internal override DbCommand ConstructSelectOneCommand(Database db, MoUserRole entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_SELECT_ONE);

            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString, entity.Uid);
            db.AddInParameter(dbCommand, "RoleId", DbType.AnsiString, entity.RoleId);
            return dbCommand;
        }

        internal override void PopulateEntityByDataReader(IDataReader reader, ref MoUserRole entity)
        {
            if (!reader.IsDBNull(0)) entity.Uid = reader.GetString(0);
            if (!reader.IsDBNull(1)) entity.RoleId = reader.GetString(1);
        }



        internal override DbCommand ConstructSelectConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region Count模块 Helper

        internal override DbCommand ConstructCountCommand(Database db)
        {
            return db.GetSqlStringCommand(SQL_COUNT);
        }

        internal override DbCommand ConstructCountConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_COUNT + condition);
        }

        #endregion

        #region IsExist模块 Helper

        internal override DbCommand ConstructIsExistCommand(Database db, MoUserRole entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_EXIST);
            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString, entity.Uid);
            db.AddInParameter(dbCommand, "RoleId", DbType.AnsiString, entity.RoleId);
            return dbCommand;
        }

        #endregion



    }
}
#endregion

